<template>
  <div class="setting">
    <!-- 卡片 -->
    <el-card>
      <!-- tabs栏 -->
      <el-tabs v-model="activeName">
        <el-tab-pane label="角色管理" name="first">
          <el-button type="primary" icon="el-icon-plus" @click="isShow = true"
            >新增角色</el-button
          >
          <el-table :data="tableData" border style="width: 100%">
            <el-table-column type="index" label="序号" width="50">
            </el-table-column>

            <el-table-column prop="name" label="角色名"> </el-table-column>
            <el-table-column prop="description" label="描述"> </el-table-column>
            <el-table-column prop="address" label="操作">
              <template #default="{row}">
                <el-button type="text">分配权限</el-button>
                <el-button @click="enterEdit(row)" type="text">修改</el-button>

                <el-popconfirm
                  title="这是一段内容确定删除吗？"
                  @onConfirm="onConfirm(row)"
                >
                  <el-button type="text" slot="reference">删除</el-button>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page"
            :page-sizes="[1, 2, 3]"
            :page-size="pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </el-tab-pane>
        <el-tab-pane label="公司信息" name="second">
          <ComInfoVue></ComInfoVue>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <SetDialogVue
      :visible="isShow"
      @update:visible="isShow = $event"
      :form="form"
      @getData="getData"
      @clear="clearHandler"
    ></SetDialogVue>
  </div>
</template>

<script>
import ComInfoVue from './components/ComInfoVue.vue'
import SetDialogVue from './components/SetDialogVue.vue'

import { getRoles, getRoleById, delRoleById } from '@/api/setting'
export default {
  components: {
    ComInfoVue,
    SetDialogVue
  },
  name: 'setting',
  data () {
    return {
      activeName: 'first',
      // 默认显示页数
      page: 1,
      tableData: [],
      // 总数据页面
      total: 1,
      pagesize: 2, //总容量
      isShow: false,
      form: {
        name: '',
        description: ''
      }
    }
  },
  created () {
    this.getData()
  },
  methods: {
    // 删除数据
    async onConfirm (row) {
      // console.log(row)
      await delRoleById(row.id)
      // 删除不做这个判断的话 获取的是当页数据 当页数据没了还是在当页
      if (this.tableData.length === 1 && this.page != 1) {
        this.page--
      }
      this.$message.success('删除成功')
      this.getData()
    },
    // 弹框关闭之后启动
    clearHandler () {
      this.form = {
        name: '',
        description: ''
      }
    },
    // 修改页面
    async enterEdit (row) {
      console.log(row)
      const res = await getRoleById(row.id)
      this.form = res
      this.isShow = true
    },
    async getData () {
      //  获取页面信息 tab栏
      const res = await getRoles({ page: this.page, pagesize: this.pagesize })
      // console.log(res)
      this.tableData = res.rows
      // 保存总数据页面
      this.total = res.total
    },
    // 页容量
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.pagesize = val
      this.page = 1
      this.getData()
    },
    // 页码改变
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.page = val
      this.getData()
    }
  }
}
</script>

<style lang="scss" scoped>
.setting {
  .el-form-item__content {
    width: 600px;
  }
  .el-pagination {
    text-align: right;
  }
}
</style>
